package com.feiyuan.movie.webmagic.eightens;

import com.feiyuan.movie.vo.SearchMovieDetailVO;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.processor.PageProcessor;

import java.util.ArrayList;
import java.util.List;

public class SearchMovieDetailProcessor implements PageProcessor {

    private Site site = Site.me().setRetryTimes(3).setSleepTime(1000);

    @Override
    public void process(Page page) {
        System.out.println(page.getHtml().toString());
        String jisuyun = page.getHtml().xpath("//div[@class='myui-panel-box clearfix'][2]//div[@class='myui-panel_hd']//h3/text()").toString();
        List<String> collectionList = new ArrayList<>();
        List<String> collectionHrefList = new ArrayList<>();
        if (jisuyun.equals("极速云")) {
            // 集数列表
            collectionList = page.getHtml().xpath("//div[@class='myui-panel-box clearfix'][2]//div[@class='tab-content myui-panel_bd']//ul//li//a/text()").all();
            // 集数地址
            collectionHrefList = page.getHtml().xpath("//div[@class='myui-panel-box clearfix'][2]//div[@class='tab-content myui-panel_bd']//ul//li//a/@href").all();
        } else {
            // 集数列表
            collectionList = page.getHtml().xpath("//div[@class='myui-panel-box clearfix'][1]//div[@class='tab-content myui-panel_bd']//ul//li//a/text()").all();
            // 集数地址
            collectionHrefList = page.getHtml().xpath("//div[@class='myui-panel-box clearfix'][1]//div[@class='tab-content myui-panel_bd']//ul//li//a/@href").all();
        }

        List<SearchMovieDetailVO> searchMovieDetailList = new ArrayList<>();
        for (int i=0;i<collectionList.size();i++) {
            SearchMovieDetailVO searchMovieDetailVO = new SearchMovieDetailVO();
            searchMovieDetailVO.setCollectionName(collectionList.get(i));
            searchMovieDetailVO.setCollectionHref(collectionHrefList.get(i));
            searchMovieDetailList.add(searchMovieDetailVO);
        }
        page.putField("list", searchMovieDetailList);
    }

    @Override
    public Site getSite() {
        return site;
    }
}
